package com.lagou.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;

/**
 *
 * 事务管理器工具类 ：开启事务、提交事务、回滚事务、释放资源
 *    通知类
 *
 * @Author wsl
 * @Date 2021/11/11 6:53
 * @Project_Name jdbc_spring
 */
@Component("transactionManager")
@Aspect //该类是个切面类
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;

    @Around("execution(* com.lagou.service.impI.AccountServiceImpI.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws SQLException {

        Object proceed = null;
        try {
            //beginTransaction();//手动开启事务
            //手动开启事务
            connectionUtils.getThreadConnection().setAutoCommit(false);

            //切入点方法执行
            proceed = pjp.proceed();

            //commit(); //手动提交事务
            //手动提交事务
            connectionUtils.getThreadConnection().commit();

        } catch (Throwable throwable) {
            throwable.printStackTrace();
            //rollback(); //手动回滚事务
            //手动回滚事务
            connectionUtils.getThreadConnection().rollback();
        } finally {
            //release(); //手动释放资源
            //手动提交改回自动提交事务
            connectionUtils.getThreadConnection().setAutoCommit(true);
            //归还连接池
            connectionUtils.getThreadConnection().close();
            //解除线程绑定
            connectionUtils.removeTreadConnection();
        }

        return proceed;
    }

    /**
     * 1.开启事务
     */
    public void beginTransaction(){
        //1.获取Connection连接
        Connection threadConnection = connectionUtils.getThreadConnection();
        try {
            //2.开启了一个手动事务
            threadConnection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 2.提交事务
     */
    public void commit(){
        //1.获取Connection连接
        Connection threadConnection = connectionUtils.getThreadConnection();
        try {
            threadConnection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 3.回滚事务
     */
    public void rollback(){
        //1.获取Connection连接
        Connection threadConnection = connectionUtils.getThreadConnection();
        try {
            threadConnection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 4.释放资源
     */
    public void release(){
        //1.获取Connection连接
        Connection threadConnection = connectionUtils.getThreadConnection();
        //2.手动提交改回自动提交事务
        try {
            threadConnection.setAutoCommit(true);
            //3.归还连接池
            connectionUtils.getThreadConnection().close();
            //4.解除线程绑定
            connectionUtils.removeTreadConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
